package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.controller.util.SaltMD5Util;
import com.example.domain.Admin;
import com.example.domain.User;
import com.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/page/{page}/{size}")
    public Data getPage(@PathVariable Integer page, @PathVariable Integer size) {
        IPage<Admin> pageUser = adminService.getPageAdmin(page, size);
        Integer code = pageUser != null ? Code.Code_ok : Code.Code_err;
        String msg = pageUser != null ? "查询成功" : "查询失败";
        return new Data(code, pageUser, msg);
    }

    @GetMapping
    public com.example.controller.util.Data getAll() {
        List<Admin> list = adminService.list();
        Integer code = list == null ? Code.Code_err : Code.Code_ok;
        String msg = list == null ? "query_err" : "query_ok";
        System.out.println(list);
        return new Data(code, list, msg);
    }

    @GetMapping("/{id}")
    public Data getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        Integer code = admin != null ? Code.Code_ok : Code.Code_err;
        String msg = admin != null ? "查询成功" : "查询失败";
        return new Data(code, admin, msg);
    }


    //    注册成为管理员
//    @PostMapping
//    public Data saveAdmin(@RequestBody Admin admin) {
//        if (adminService.agency()) {
//            adminService.saveAdmin(admin);
//            return new Data(200, "添加代理成功！");
//        }
//        return new Data(205, "您的权限不够！无法添加");
//    }
//    添加的代码
    @PostMapping
    public Data saveAdmin(@RequestBody Admin admin) {
        Admin admin1 = adminService.getAdmin(admin.getName());
        if (admin1 != null) {
            return new Data(201, "该用户已存在");
        }
//  Todo 将密码进行md5加盐加密
        String password = admin.getPassword();
        System.out.println("ok"+password);
        String saltPassword = SaltMD5Util.generateSaltPassword(password);
        admin.setPassword(saltPassword);
        System.out.println(saltPassword);
        boolean save = adminService.save(admin);
        if (save == false) {
            return new Data(202, "用户添加异常！请重试");
        }
        return new Data(200, "添加成功");
    }


    //管理员登录
    @PostMapping("/login")
    public Data loginByPassword(@RequestBody Admin admin) {
        Data result = adminService.login(admin);
        return result;
    }

    @DeleteMapping("/{id}")
    public Data delete(@PathVariable Integer id) {
        if (adminService.agency()) {
            boolean result = adminService.removeById(id);
            Integer code = result != false ? Code.Code_ok : Code.Code_err;
            String msg = result != false ? "删除成功" : "删除失败";
            return new Data(code, msg);
        }
        return new Data(205, "您的权限不够！");
    }

    @PutMapping()
    public Data updateById(@RequestBody Admin admin) {
        boolean b = adminService.updateById(admin);
        Integer code = b == true ? Code.Code_ok : Code.Code_err;
        String msg = b == true ? "更新成功" : "更新失败";
        return new Data(code, msg);
    }
}
